package com.apes.fn.scm.used.server;

import com.apes.fn.scm.used.model.Used;
import com.apes.fn.scm.used.model.UsedStock;
import com.apes.fn.scm.used.model.UsedStore;
import com.apes.fn.scm.used.repository.UsedStockRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.jpa.spec.model.Filter;
import com.apes.framework.jpa.spec.model.Group;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.service.UomService;
import com.apes.scm.masterdata.stock.model.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("usedStockService")
public class UsedStockService extends DomainService {
    @Autowired
    UsedStockRepository usedStockRepository;

    @Autowired
    UomService uomService;

    /**
     * 写旧件库存
     *
     * @param used
     * @param location
     * @param changeQty
     * @param company
     */
    public void setUsedStock(Used used, Location location, double changeQty, PartyRole company, Boolean isNormal) {

        UsedStock usedStock = getOrCreateProductAccount(used, location, company);
        if( isNormal ) {
            usedStock.addStockQty(changeQty);
        } else {
            usedStock.addInsuranceStockQty(changeQty);
        }
        usedStockRepository.saveAndFlush(usedStock);
    }


//    /**
//     * 功能：计算基本单位数量
//     *
//     * @param company
//     * @param used
//     * @param uom
//     * @param changeQty
//     * @return
//     */
//    private double computeQtyUnit(PartyRole company, Used used, ProductUom uom, double changeQty) {
//        ProductUom baseUom = uomService.getUom(company, used.getProduct());
//        if (baseUom == null) throw new RuntimeException("商品：" + used.getProduct().getName() + " 没有定义基本计量单位！");
//        if (uom.getId() != baseUom.getId()) {
//            return uom.computeQty(changeQty, baseUom, true);
//        }
//        return changeQty;
//    }


    private UsedStock getOrCreateProductAccount(Used used, Location location, PartyRole company) {
        UsedStock usedStock = usedStockRepository.findByUsedAndLocation(used, location);
        if (usedStock == null) {
            usedStock = new UsedStock();
            usedStock.setUsed(used);
            usedStock.setLocation(location);
            usedStock.setCompany(company);
        }
        return usedStock;
    }

    public UsedStock getStockQty(SimpleRequest request) {
        Location location = new Location();
        Used used = new Used();
        location.setId(request.get("locationId"));
        used.setId(request.get("usedId"));
        return getStockQty(location, used);
    }


    /***
     * 功能：通过仓库和商品获取可用库存
     * @param location
     * @param used
     * @return
     */
    public UsedStock getStockQty(Location location, Used used) {
        List<UsedStock> usedStocks = usedStockRepository.findAll(JpaDsl.toCriteria(MapUtil.mapper("location", location, "used", used)));

        if(usedStocks != null && usedStocks.size() > 0) {
            return usedStocks.get(0);
        } else {
            return null;
        }
    }


}
